/** --------此文件用于判断一些类型基本信息 ------------------- */
// 判断是否还是一个对象
const isObject = obj => {
  return Object.prototype.toString.call(obj) === '[object Object]'
}

// 扁平化对象
const getObjKeys = obj => {
  const result = {}
  const helpLoop = obj => {
    const objKeys = Object.keys(obj)
    for (let i = 0; i < objKeys.length; i ++) {
      if (typeof obj[objKeys[i]] === 'object') {
        helpLoop(obj[objKeys[i]])
      } else {
        result[objKeys[i]] = obj[objKeys[i]]
      }
    }
  }
  helpLoop(obj)
  return result
}

// 比较两个对象差异
const comparisonObject = (objPre, objNext) => {
  if (Object.keys(objPre).length === 0 || ! objPre || ! objNext) return false
  const tempPre = getObjKeys(objPre)
  const tempNext = getObjKeys(objNext)
  // 数组去重
  let keys = []
  keys.push(...Object.keys(tempPre))
  keys.push(...Object.keys(tempNext))
  keys = [ ...new Set(keys) ]
  for (let i = 0; i < keys.length; i ++) {
    if (tempPre[keys[i]] !== tempNext[keys[i]]) {
      return keys[i]
    }
  }
  return false
}

export { isObject, comparisonObject }
